package com.ruoyi.erecord.samplebox.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.erecord.samplebox.domain.bo.SampleboxImportBaseBo;
import com.ruoyi.erecord.samplebox.domain.vo.SampleboxImportBaseVo;
import com.ruoyi.erecord.samplebox.domain.SampleboxImportBase;
import com.ruoyi.erecord.samplebox.mapper.SampleboxImportBaseMapper;
import com.ruoyi.erecord.samplebox.service.ISampleboxImportBaseService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 样本框导入Service业务层处理
 *
 * @author cc
 * @date 2023-04-22
 */
@RequiredArgsConstructor
@Service
public class SampleboxImportBaseServiceImpl implements ISampleboxImportBaseService {

    private final SampleboxImportBaseMapper baseMapper;

    /**
     * 查询样本框导入
     */
    @Override
    public SampleboxImportBaseVo queryById(String batchNo){
        return baseMapper.selectVoById(batchNo);
    }

    /**
     * 查询样本框导入列表
     */
    @Override
    public TableDataInfo<SampleboxImportBaseVo> queryPageList(SampleboxImportBaseBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SampleboxImportBase> lqw = buildQueryWrapper(bo);
        Page<SampleboxImportBaseVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询样本框导入列表
     */
    @Override
    public List<SampleboxImportBaseVo> queryList(SampleboxImportBaseBo bo) {
        LambdaQueryWrapper<SampleboxImportBase> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SampleboxImportBase> buildQueryWrapper(SampleboxImportBaseBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SampleboxImportBase> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBatchNo()), SampleboxImportBase::getBatchNo, bo.getBatchNo());
        lqw.eq(bo.getProcessStatus() != null, SampleboxImportBase::getProcessStatus, bo.getProcessStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getProcessMsg()), SampleboxImportBase::getProcessMsg, bo.getProcessMsg());
        return lqw;
    }

    /**
     * 新增样本框导入
     */
    @Override
    public Boolean insertByBo(SampleboxImportBaseBo bo) {
        SampleboxImportBase add = BeanUtil.toBean(bo, SampleboxImportBase.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setBatchNo(add.getBatchNo());
        }
        return flag;
    }

    /**
     * 修改样本框导入
     */
    @Override
    public Boolean updateByBo(SampleboxImportBaseBo bo) {
        SampleboxImportBase update = BeanUtil.toBean(bo, SampleboxImportBase.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SampleboxImportBase entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除样本框导入
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
